﻿using System;
using System.Threading;
using System.Collections.Generic;
struct Object
{
    public int x;
    public int y;
    public char c;
    public ConsoleColor color;
}
class Program
{
    static void PrintOnPosition(int x, int y, char c, ConsoleColor color = ConsoleColor.Red)
    {
        Console.SetCursorPosition(x, y);
        Console.ForegroundColor = color;
        Console.Write(c);
    }
    
    static void Main(string[] args)
    {
        ConsoleColor[] col = { ConsoleColor.Red, ConsoleColor.Green, ConsoleColor.Blue, ConsoleColor.Yellow, ConsoleColor.White, ConsoleColor.Gray };
        int playfieldWidth = 30;
        Console.Write("Enter game dificultu(Easy, Normal , Hard , Chuck Norris): ");
        int countLives = 0;
        int speed = 0;
        string choice = Console.ReadLine();
        switch (choice)
        {
            case "Easy" :
                countLives = 10;
                speed = 150;
                break;
            case "Normal":
                countLives = 5;
                speed = 100;
                break;
            case "Hard" :
                countLives = 3;
                speed = 50;
                break;
            case "Chuck Norris" :
                countLives = 1;
                speed = 5;
                break;
            case "yolo":
                countLives = 1;
                speed = 0;
                break;
            case "Nuck Chorris":
                countLives = 1;
                speed = 2;
                break;
            default:
                Console.WriteLine("Enter a valid mode");
                break;
        }
        begining:
        Console.WriteLine("Let the game begin in 3 ");
        Thread.Sleep(1000);
        Console.Clear();

        Console.WriteLine("2");
        Thread.Sleep(1000);
        Console.Clear();

        Console.WriteLine("1");
        Thread.Sleep(1000);
        Console.Clear();


        Console.BufferHeight = Console.WindowHeight = 30;
        Console.BufferWidth = Console.WindowWidth = 30;
        Object userObject = new Object();
        userObject.x = 13;
        userObject.y = Console.WindowHeight - 1;
        userObject.c = '0';
        userObject.color = ConsoleColor.Green;
        Random randomGenerator = new Random();
        List<Object> fallingObjects = new List<Object>();
        bool haveLives = true;
        long score = 0;
        while (haveLives)
        {
            {
                char[] ch = { '@', '#', '$', '%', '&', '*','.','!','+','-',';' };
                Object newObject = new Object();
                newObject.color = col[randomGenerator.Next(0, 6)];
                newObject.x = randomGenerator.Next(0,playfieldWidth);
                newObject.y = 0;
                newObject.c = ch[randomGenerator.Next(0, 11)];
                fallingObjects.Add(newObject);
            }
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo pressedKey = Console.ReadKey(true);
                while (Console.KeyAvailable) Console.ReadKey(true);
                if ((pressedKey.Key == ConsoleKey.LeftArrow || pressedKey.Key == ConsoleKey.A) && (userObject.x - 1 >= 0))
                {
                    userObject.x = userObject.x - 1;
                }
                else if ((pressedKey.Key == ConsoleKey.RightArrow||pressedKey.Key == ConsoleKey.D)&&(userObject.x + 1 < playfieldWidth))
                {
                    userObject.x = userObject.x +1;
                }
                else if ((pressedKey.Key == ConsoleKey.UpArrow||pressedKey.Key == ConsoleKey.W)&&(userObject.y - 1 > 0))
                {
                    userObject.y = userObject.y - 1;
                }
                else if ((pressedKey.Key == ConsoleKey.DownArrow || pressedKey.Key == ConsoleKey.S) && (userObject.y + 1 < 30))
                {
                    userObject.y = userObject.y + 1;
                }

            }
            List<Object> newList = new List<Object>();
            for (int i = 0; i < fallingObjects.Count; i++)
            {
                Object oldObject = fallingObjects[i];
                Object newObject = new Object();
                newObject.x = oldObject.x;
                newObject.y = oldObject.y + 1;
                newObject.c = oldObject.c;
                newObject.color = oldObject.color;
                if (newObject.y == userObject.y && newObject.x == userObject.x)
                {
                    if (choice=="Nuck Chorris")
	                {
                        if (countLives<20)
                        {
                            countLives++;
                        }
	                }
                    else
	                {
                        countLives--;
	                }               
                }
                else
                {
                    score++;
                }
                if (newObject.y<Console.WindowHeight)
                {
                    newList.Add(newObject);
                }
                
            }
           
            fallingObjects = newList;
            Console.Clear();
            PrintOnPosition(userObject.x, userObject.y, userObject.c, userObject.color);
            foreach (Object obj in fallingObjects)
            {
                PrintOnPosition(obj.x, obj.y, obj.c, obj.color);
            }  
 
            if (17000>score&&score>15000)
            {
                if (speed>0)
                {
                    speed--;
                }
            }
            
            Console.Title = String.Format("Your lives : {0}", countLives);
            Thread.Sleep(speed);
            if ((choice == "Nuck Chorris") && (countLives >= 20))
            {
                haveLives = false;
                Console.Clear();
                Console.WriteLine("Congratz! Try dying!");
                Console.WriteLine("Your score is {0}.", score);
                Console.WriteLine("Start over?");
                string answer = Console.ReadLine();
                if (answer == "yes")
                {
                    countLives = 1;
                    haveLives = true;
                }
                if (answer == "secret")
                {
                    Console.WriteLine("yolo");
                    Console.WriteLine("Nhuck Corris");
                }
            }
            else if (countLives <= 0)
            {
            
                haveLives = false;
                Console.Clear();
                Console.WriteLine("GAME OVER!");
                Console.WriteLine("Your score is {0}.",score);
                Console.WriteLine("Start over?");
                string answer = Console.ReadLine();
                if (answer == "yes")
                {
                    countLives = 10;
                    haveLives = true;
                    score = 0;
                    goto begining;
                }
                if (answer=="secret")
	            {
		            Console.WriteLine("yolo");
                    Console.WriteLine("Nhuck Corris");
	            }
            }
        }
        Console.ReadLine();
    }
}